/*
 * Filename:    AuditDocument.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-12-10
 */
package com.paic.mhis.hcpms.audit.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.itextpdf.web.pdfmodel.DocumentModel;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsFontUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsParagraphUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsPdfCellUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsPdfTableUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsRectangleUtil;

public class AuditDocument implements DocumentModel {

	@Override
	public void createDocument(Document document) {
		document.setPageSize(HcpmsRectangleUtil.createNoticeRect());
	}

	@Override
	public void createContent(Document document, Map<String, Object> map) {

		Font font = HcpmsFontUtil.createTitleFont(12, Font.BOLD);
		Font fontText = HcpmsFontUtil.createTextFont(10, Font.NORMAL);

		try {
			float[] widths = { 0.15f, 0.3f, 0.15f, 0.4f };
			PdfPTable table = HcpmsPdfTableUtil.createTable(widths, 10, 90,
					Element.ALIGN_CENTER);
			create(map, font, fontText, table);
			document.add(table);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	private void create(Map<String, Object> map, Font font, Font fontText,
			PdfPTable table) {
		table.addCell(HcpmsPdfCellUtil.createCellCol(
				HcpmsParagraphUtil.createParagraphText(font,
						getValue(map, AuditConstants.title)), new int[] { 5, 5,
						5, 5 }, Element.ALIGN_CENTER, 4, Rectangle.NO_BORDER,
				30f));
		Font remarkFont = addPdfCell(map, fontText, table);

		StringBuffer sql = new StringBuffer();
		sql.append("请机构法定代表人或负责人接到本告知书之日起三日内，到社保中心接受稽核处理，办理签字程序，请携带以下材料\n");
		sql.append("1.机构公章\n");
		sql.append("2.若法定代表人无法到场,请携带法定代表人签署的授权委托书.\n");
		sql.append("3.被委托人的身份证号原件、复印件");

		table.addCell(HcpmsPdfCellUtil.createCellCol(HcpmsParagraphUtil
				.createParagraphText(fontText, sql.toString()), new int[] { 1,
				10, 4, 40 }, Element.ALIGN_LEFT, 4, Rectangle.NO_BORDER));

		// /签章
		PdfPCell cell1 = HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(remarkFont, "机构签章:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER);
		cell1.setFixedHeight(50f);
		table.addCell(cell1);
		PdfPCell cell2 = HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, ""),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT,
				Rectangle.NO_BORDER);
		cell2.setFixedHeight(50f);
		table.addCell(cell2);
		PdfPCell cell3 = HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(remarkFont, "签章日期:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER);
		cell3.setFixedHeight(50f);
		table.addCell(cell3);
		PdfPCell cell4 = HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.sealDate)), new int[] { 4,
						4, 4, 1 }, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
		cell4.setFixedHeight(50f);
		table.addCell(cell4);
	}

	private Font addPdfCell(Map<String, Object> map, Font fontText,
			PdfPTable table) {
		// 1
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, "函件编号:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.CASE_CODE)), new int[] {
						4, 4, 4, 1 }, Element.ALIGN_LEFT, Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, "机构名称:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.healthcareProviderName)),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT,
				Rectangle.NO_BORDER));
		// 2
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, "机构法人:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.lealPerson)), new int[] {
						4, 4, 4, 1 }, Element.ALIGN_LEFT, Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, "机构地址:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.fullAddress)), new int[] {
						4, 4, 4, 1 }, Element.ALIGN_LEFT, Rectangle.NO_BORDER));
		// 3
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, "定点时间:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.pointStartDate)),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, "案发时间:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.auditDate)), new int[] {
						4, 4, 4, 1 }, Element.ALIGN_LEFT, Rectangle.NO_BORDER));
		// 4
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, "稽核时段:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCellCol(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.auditRangDate)),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT, 3,
				Rectangle.NO_BORDER));
		// 5
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, "稽核摘要:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCellCol(
				HcpmsParagraphUtil.createParagraphText(fontText, ""),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT, 3,
				Rectangle.NO_BORDER));

		table.addCell(HcpmsPdfCellUtil.createCellCol(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.auditSummary)), new int[] {
						1, 10, 4, 40 }, Element.ALIGN_LEFT, 4,
				Rectangle.NO_BORDER));
		// 6
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(fontText, "处理意见:  "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCellCol(
				HcpmsParagraphUtil.createParagraphText(fontText, ""),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT, 3,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCellCol(
				HcpmsParagraphUtil.createParagraphText(fontText,
						getValue(map, AuditConstants.opinionDesc)), new int[] {
						1, 10, 4, 40 }, Element.ALIGN_LEFT, 4,
				Rectangle.NO_BORDER));
		// 7
		Font remarkFont = HcpmsFontUtil.createTitleFont(10, Font.BOLD);
		table.addCell(HcpmsPdfCellUtil.createCell(
				HcpmsParagraphUtil.createParagraphText(remarkFont, "备注： "),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
				Rectangle.NO_BORDER));
		table.addCell(HcpmsPdfCellUtil.createCellCol(
				HcpmsParagraphUtil.createParagraphText(fontText, ""),
				new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT, 3,
				Rectangle.NO_BORDER));
		return remarkFont;
	}

	@Override
	public void setPdfWriter(PdfWriter pdf, Map<String, Object> map) {
	}

	public static String getValue(Map<String, Object> map, String name) {
		String str = "";
		if (name == null || "".equals(name))
			return str;
		Object obj = map.get(name);
		if (obj == null || "".equals(obj))
			return str;
		if (obj instanceof Date) {
			SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
			str = sft.format(obj);
		} else if (obj instanceof List) {

		} else if (obj instanceof Map) {

		} else {
			str = obj.toString();
		}
		return str;
	}
}
